<?php
namespace Home\Controller;
class AdminController extends AuthController {
    public function index(){
        $data['visite'] = M('GameRecord')->where(array('date'=>array('gt',date('Y-m-d',time()))))->count();
        $data['new_visite'] = M('GameRecord as gr')
            ->join('left join wxg_users as u on gr.user_id = u.id')
            ->where(array('gr.date'=>array('gt',date('Y-m-d',time())),'u.date'=>array('gt',date('Y-m-d',time()))))
            ->count();
        $data['time'] = M('GameRecord')->where(array('date'=>array('gt',date('Y-m-d',time()))))->avg('play_time');
        $data['old_visite'] = $data['visite']-$data['new_visite'];
        $this->assign($data);
       $this->display();
    }
    function getData(){
        $date = I('get.time');
        switch ($date){
            case 'today':{
                $where = array('date'=>array('gt',date('Y-m-d',time())));
                $where1 = array('gr.date'=>array('gt',date('Y-m-d',time())),'u.date'=>array('gt',date('Y-m-d',time())));
                break;
            }
            case 'yes':{
                $where = array('date'=>array('between',array(date('Y-m-d',strtotime('-1 day')),date('Y-m-d',time()))));
                $where1 = array('gr.date'=>array('between',array(date('Y-m-d',strtotime('-1 day')),date('Y-m-d',time()))),'u.date'=>array('between',array(date('Y-m-d',strtotime('-1 day')),date('Y-m-d',time()))));
                break;
            }
            case 'week':{
                $where = array('date'=>array('gt',date('Y-m-d',strtotime('-1 week'))));
                $where1 = array('gr.date'=>array('gt',date('Y-m-d',strtotime('-1 week'))),'u.date'=>array('gt',date('Y-m-d',strtotime('-1 week'))));
                break;
            }
            case 'month':{
                $where = array('date'=>array('gt',date('Y-m-d',strtotime('-1 month'))));
                $where1 = array('gr.date'=>array('gt',date('Y-m-d',strtotime('-1 month'))),'u.date'=>array('gt',date('Y-m-d',strtotime('-1 month'))));
                break;
            }
            default:{
                break;
            }
        }
        $data['visite'] = M('GameRecord')->where($where)->count();
        $data['new_visite'] =
            M('GameRecord as gr')
            ->join('left join wxg_users as u on gr.user_id = u.id')
            ->where($where1)
            ->count();
        $data['time'] = M('GameRecord')->where($where)->avg('play_time');
        $data['time'] = intval($data['time']);
        $data['old_visite'] = $data['visite']-$data['new_visite'];
        exit(json_encode($data));
    }

    function welcome(){
        $this->display('welcome');
    }
}